package com.epfresh.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.activity.DetailGoodsActivity;
import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.bean.HomeCommTag;
import com.epfresh.bean.ZhengHuoStoreProducts;
import com.epfresh.global.BaseApplication;
import com.epfresh.global.BaseFragment;
import com.epfresh.views.stick.OnStickyListener;
import com.epfresh.views.tag.TagCloudView;

import java.util.ArrayList;
import java.util.List;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

/**
 * Created by wzh on 16-11-11.
 */

public class CommonStoreGoodsFragment extends BaseFragment implements OnStickyListener {


    private ListView mListView;
    Context mContext;
    private RelativeLayout vEmptyData;
    private CommonAdapter adapter;
    public boolean isAcceptOrders = true;
    private List<ZhengHuoStoreProducts> list = new ArrayList<>();

    public void updateData(List<ZhengHuoStoreProducts> list) {
        this.list.clear();
        this.list.addAll(list);
        if (mListView != null && vEmptyData != null) {
            if (this.list == null || this.list.size() == 0) {
                mListView.setVisibility(GONE);
                vEmptyData.setVisibility(VISIBLE);
            } else {
                mListView.setVisibility(VISIBLE);
                vEmptyData.setVisibility(GONE);
            }
        }
        if (adapter != null){
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_common_store_goods, container, false);
        mListView = (ListView) view.findViewById(R.id.listview);
        vEmptyData = (RelativeLayout) view.findViewById(R.id.nodate_ll);
        mContext = this.getActivity();

        if (adapter == null){
            adapter = new CommonAdapter<ZhengHuoStoreProducts>(mContext, list,
                    R.layout.item_list_goods) {
                @Override
                public void convert(final ViewHolder holder, final ZhengHuoStoreProducts bean) {
                    holder.getView(R.id.tv_open_tag).setVisibility(GONE);
                    if (bean.getType() == 2) {//预售
                        holder.getView(R.id.tv_type).setVisibility(VISIBLE);
                        String html="<font color=\"#00ffffff\">a预售】</font>"+bean.getTitle();
                        ((TextView) (holder.getView(R.id.tv_title))).setText(Html.fromHtml(html));
                    } else {//现售
                        holder.getView(R.id.tv_type).setVisibility(GONE);
                        holder.setText(R.id.tv_title, bean.getTitle());
                    }
//                    String[] price = bean.getPrice().split("元");
                    String price = bean.getPrice();

                    holder.setText(R.id.tv_price, ""+price);
//                    holder.setText(R.id.tv_price_, "元"+price[1]);
//                    holder.setText(R.id.tv_price_, "元");
                    if (!TextUtils.isEmpty(bean.getPack())) {
                        holder.setText(R.id.tv_unit_package, bean.getPack());
                        holder.getView(R.id.tv_unit_package).setVisibility(VISIBLE);
                    } else {
                        holder.setText(R.id.tv_unit_package, "");
                        holder.getView(R.id.tv_unit_package).setVisibility(GONE);
                    }
                    String promotionType = bean.getPromotionItemType();
                    String state = bean.getPromotionItemStatus();

                    Log.e("promotionType","promotionType:"+promotionType);

                    holder.getView(R.id.tv_price_old).setVisibility(GONE);
                    if ("1".equals(promotionType) || "2".equals(promotionType)) {//1促销2秒杀
                            holder.setText(R.id.tv_price_old,bean.getOriginalPrice() + "元/" + bean.getChargeUnit());
                            holder.getView(R.id.tv_price_old).setVisibility(VISIBLE);
                            ((TextView)holder.getView(R.id.tv_price_old)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);


                        if ("1".equals(promotionType)) {//1促销2秒杀
                            holder.getView(R.id.tv_activity_promotion).setVisibility(View.VISIBLE);
                            holder.getView(R.id.tv_activity_second).setVisibility(View.GONE);
                        } else if ("2".equals(promotionType)) {//1促销2秒杀
                            holder.getView(R.id.tv_activity_promotion).setVisibility(View.GONE);
                            holder.getView(R.id.tv_activity_second).setVisibility(View.VISIBLE);
                        } else {
                            holder.getView(R.id.tv_activity_promotion).setVisibility(View.GONE);
                            holder.getView(R.id.tv_activity_second).setVisibility(View.GONE);
                        }
                    }else{
                        holder.getView(R.id.tv_activity_promotion).setVisibility(View.GONE);
                        holder.getView(R.id.tv_activity_second).setVisibility(View.GONE);
                        holder.getView(R.id.tv_price_old).setVisibility(GONE);
                    }
                    ArrayList<HomeCommTag> tags = bean.getTags();
                    if (tags != null && tags.size() > 0) {
                        TagCloudView tag;
                        tag = holder.getView(R.id.ll_tags);
                        tag.setVisibility(View.VISIBLE);
                        tag.setCommTags(tags);
                    } else {
                        holder.getView(R.id.ll_tags).setVisibility(View.GONE);
                    }

                    if ("1".equals(promotionType)) {//1促销2秒杀
                        if("4".equals(state)){
                            ((TextView)holder.getView(R.id.tv_open_tag)).setText("抢光了");
                            holder.getView(R.id.tv_open_tag).setVisibility(VISIBLE);
                        }
                    }else  if ("2".equals(promotionType)) {//1促销2秒杀
                        if("4".equals(state)){
                            ((TextView)holder.getView(R.id.tv_open_tag)).setText("抢光了");
                            holder.getView(R.id.tv_open_tag).setVisibility(VISIBLE);
                        }
                    }
                    ImageLoader.getInstance().displayImage(bean.getThumbnail(), (ImageView) holder.getView(R.id.iv_pic));

                    String promotionMarkUrl = bean.getPromotionMarkUrl();
                    ImageLoader.getInstance().displayImage(promotionMarkUrl, new ImageViewAware((ImageView) holder.getView(R.id.iv_promotion)), BaseApplication.getTagOptions(promotionMarkUrl) , null, null, null);

                    if(!isAcceptOrders){
                        ((TextView)holder.getView(R.id.tv_open_tag)).setText("暂不接单");
                        holder.getView(R.id.tv_open_tag).setVisibility(VISIBLE);
                    }

                    holder.setOnClickListener(R.id.item, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, DetailGoodsActivity.class);
                            intent.putExtra("id", bean.getId());
                            intent.putExtra("storeId",bean.getStoreId() + "");
                            intent.putExtra("promotionItemId", bean.getPromotionItemId());
                            startActivity(intent);
                        }
                    });

                }
            };
        }
        mListView.setAdapter(adapter);

        mListView.setOnScrollListener(onScrollListener);

        return view;
    }

    boolean isTop = true;
    boolean isBottom;
    AbsListView.OnScrollListener onScrollListener = new AbsListView.OnScrollListener() {

        @Override

        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }



        @Override

        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            isTop = false;
            isBottom = false;
            //滑到顶部
            if (firstVisibleItem == 0) {
                isTop = true;
                isBottom = false;
            }
            //滑到底部
            if (visibleItemCount + firstVisibleItem == totalItemCount) {
                isTop = false;
                isBottom = true;
            }

        }

    };

    @Override
    public boolean isTop() {

        if (mListView != null){
            if (mListView.getChildCount() > 0 ) {
                Log.e("mlistview","FirstVisiblePosition:"+mListView.getFirstVisiblePosition());
                Log.e("mlistview","ChildAt:"+mListView.getChildAt(0).getTop());
                Log.e("mlistview","getPaddingTop:"+mListView.getPaddingTop());
                Log.e("mlistview","canScrollVertically:"+mListView.canScrollVertically(-1));

                return (mListView.getFirstVisiblePosition() == 0 && mListView.getChildAt(0)
                        .getTop() == mListView.getPaddingTop() /*&& !mListView.canScrollVertically(-1)*/);
            }else {
                return true;
            }
        }
        return isTop;
    }

    @Override
    public void setScroll(boolean canScroll) {

    }

    @Override
    public String getBaseTag() {
        return null;
    }
}
